﻿using Microsoft.AspNetCore.Mvc;
using MVC系统mvc.Dto.ProcessProceduresDto;
using MVC系统mvc.Dto.ProcessTasks;
using MVC系统mvc.Dto.ProcessTasksDto;
using Newtonsoft.Json;
using WMS_MAV;

namespace MVC系统mvc.Controllers
{
    /// <summary>
    /// 工序管理控制器
    /// </summary>
    public class ProcessProceduresController : Controller
    {
        /// <summary>
        /// 配置信息
        /// </summary>
        protected readonly IConfiguration configuration;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configuration">配置信息</param>
        public ProcessProceduresController(IConfiguration configuration)
        {
            this.configuration = configuration;
        }

        /// <summary>
        /// 工序管理工序--视图
        /// </summary>
        /// <returns></returns>
        public IActionResult ShowProductionProcess()
        {
            return View();
        }

        /// <summary>
        /// 添加工序--视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddProductionProcess()
        {
            return View();
        }

        /// <summary>
        /// 添加工序信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpPost]
        public async Task<ActionResult> AddProductionProcessInfo(AddProcessProceduresDto dto)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(dto.Token);
            string url = configuration["Url"] + "ppapi/api/ProcessRoute/CreadProductionProcess";
            var response = await httpClientWrapper.PostAsync(url, dto.Data);
            return Json(response);
        }


        /// <summary>
        /// 工序数据分页展示列表
        /// </summary>
        /// <param name="dto">dto</param>
        /// <returns>JsonResult</returns>
        [HttpGet]
        public async Task<ActionResult> GetProductionProcessPageList(ShowProcessProceduresDto dto)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(dto.Token);
            string url = configuration["Url"] + "ppapi/api/ProcessRoute/ShowProductionProcessList?ProductionProcessesName=" + dto.ProductionProcessesName + "&ProductionProcessesNumber=" + dto.ProductionProcessesNumber+ "&Status=" + dto.Status + "&PageIndex=" + dto.PageIndex + "&PageSize=" + dto.PageSize;
            var data = JsonConvert.SerializeObject(dto);
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }

        /// <summary>
        /// 删除工序信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpPost]
        public async Task<ActionResult> DeleteProductionProcess(DelProcessProceduresDto dto)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(dto.Token);
            string url = configuration["Url"] + "ppapi/api/ProcessRoute/DeleteProductionProcesse";
            var response = await httpClientWrapper.PostAsync(url, dto.Data);
            return Json(response);
        }

        /// <summary>
        /// 批量删除工序信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpPost]
        public async Task<ActionResult> BatchDeleteProductionProcess(BatchDeleteProductionProcessDto dto)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(dto.Token);
            string url = configuration["Url"] + "ppapi/api/ProcessRoute/BatchDeleteProductionProcess";
            //var data = "{\"ids\":" + dto.ids +" , \"deletedBy\":" + dto.deletedBy + "} ";
            //var data = new StringContent(JsonSerializer.Serialize(new { ids = dto.ids, deletedBy = dto.deletedBy }), Encoding.UTF8, "application/json");
            var response = await httpClientWrapper.PostAsync(url, dto.Data);
            return Json(response);
        }


        /// <summary>
        /// 反填工序信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpGet]
        public async Task<ActionResult> ReverseProductionProcess(ReverseFillingProductionProcessDto dto)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(dto.token);
            string url = configuration["Url"] + "ppapi/api/ProcessRoute/ReverseFillingProductionProcess?ProductionProcessesId=" + dto.productionProcessesId;
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }

        /// <summary>
        /// 修改工序信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpPost]
        public async Task<ActionResult> UpdateProductionProcess(UpdateProcessProceduresDto objTwo, string token)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = configuration["Url"] + "ppapi/api/ProcessRoute/UpdateProductionProcess";
            string list = JsonConvert.SerializeObject(objTwo);
            var response = await httpClientWrapper.PostAsync(url, list);
            return Json(response);
        }
    }
}
